
clear
set more off
capture log close
set seed 12345
set matsize 10000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"


****************************
**  Prep stayers data
****************************
use $DATA/scorp100p_entsep_file, clear

* set DD year
local yy "2016"
keep if year == 2012 | year==`yy'

* prep entrants, separators, stayers
sort firm_tin tin year
gen zz = year==`yy'
by firm_tin tin: egen c`yy' = max(zz)
drop zz

drop ent
gen ent = c2012==0 & c`yy'==1
tab year ent 
gen sep = c2012==1 & c`yy'==0
tab year sep
gen stay = c2012==1 & c`yy'==1
tab year stay

gen wages_ent_ft = 0
replace wages_ent_ft = wages if ent==1 & ft==1
gen wages_sep_ft = 0
replace wages_sep_ft = wages if sep==1 & ft==1
gen wages_stay_ft = 0
replace wages_stay_ft = wages if stay==1 & ft==1
gen wages_ft=0
replace wages_ft=wages if ft==1

collapse (sum) wages_ft wages_ent_ft wages_sep_ft wages_stay_ft, by(firm_tin year)

ren wages_ft wages_all_ft

sort firm_tin year
merge 1:1 firm_tin year using $DATA/reg_file_firm.dta
tab year _merge
keep if _merge==3
drop _merge

ren wages_ft wages_ft_firm
ren wages_all_ft wages_ft

************
**  Decomp
************
* generate wage shares
gen zz = 0
replace zz=wages_ft if year==2012
bys firm_tin: egen wages2012_ft=sum(zz)
drop zz

gen zz = 0
replace zz=wages_stay_ft if year==2012
bys firm_tin: egen wages_stay2012_ft=sum(zz)
drop zz

gen dpwages_ft = (wages_ft-wages2012_ft)/wages2012_ft
gen stayshare_ft = (wages_stay_ft-wages_stay2012_ft)/wages2012_ft
gen turn_ft=wages_ft-wages_stay_ft
gen turnshare_ft = (turn_ft-(wages2012_ft-wages_stay2012_ft))/wages2012_ft


**********************
**********************
**  Table 3 - wagebill decomp
**********************
**********************
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_2012##ib`baseyr'.year"
local events "t`yy' y`yy' `t'"

gen zz = 0
replace zz=dpwages_ft if year==`yy'
bys firm_tin: egen dpwages`yy'_ft=sum(zz)
drop zz
sum dpwages`yy'_ft,d
* wins ratio p99
gen maxwft=r(p99)

foreach r in 1 {
	eststo  wages_r`r': quietly reghdfe dpwages_ft `events' [aw=wages2012_ft] if dpwages`yy'_ft<=maxwft, absorb(firm_tin `r`r'') 
	eststo stay_r`r': quietly reghdfe stayshare_ft `events'  [aw=wages2012_ft] if dpwages`yy'_ft<=maxwft, absorb(firm_tin `r`r'') 
	eststo turn_r`r': quietly reghdfe turnshare_ft `events'  [aw=wages2012_ft] if dpwages`yy'_ft<=maxwft, absorb(firm_tin `r`r'') 
	}
esttab using "$OUTPUT/table3_wagebill.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear



clear
exit
